﻿using Max.PH4.Shoppgin.Admin.Domain;
using Max.PH4.Shoppgin.Admin.DTO;
using Max.PH4.Shoppgin.Admin.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH4.Shoppgin.Admin.API.Controllers.System2
{
    /// <summary>
    /// 系统用户控制器
    /// </summary>
    [Route("system/user")]
    [ApiController]
    public class SystemUserController : ControllerBase
    {

        ISystemUserService _service;        
        public SystemUserController(ISystemUserService service)
        {
            _service = service;
        }


        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(SystemUserLoginDto dto)
        {
            ResponseResultDto result = new();

            try
            {
                var data = _service.Login(dto.Name, dto.Password);

                if(!data.Item1)
                {
                    result.Code = ResponseStateCode.Error_UserName;
                    result.Message = data.Item2;
                }

                result.Data = data.Item3;
            }
            catch (Exception ex)
            {
                result.Code = ResponseStateCode.Failed;
            }

            return Ok(result);
        }

        /// <summary>
        /// 查询所有用户数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Query(string? name="",int pageIndex = 1,int pageSize = 10)
        {
            //第一：验证参数
            if (pageIndex <= 0) pageIndex = 1;
            if(pageSize <= 0) pageSize = 10;
            name ??= "";

            ResponseResultDto result = new();
            try
            {
                //第二：调用业务
                var list = _service.Query(name,pageIndex,pageSize,out int total);

                result.Data = new { items = list, total };
            }
            catch (Exception)
            {
                result.Code = ResponseStateCode.Failed;                
            }

            return Ok(result);
        }

    }
}
